/*
 * Created by SharpDevelop.
 * User: nebulom
 * Date: 8/26/2009
 * Time: 7:54 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Miming.Model;
using System.Collections.Generic;
using Miming.CashRegister.Util;

namespace Miming.CashRegister.Gui
{
	/// <summary>
	/// Description of SalesReceiptPane.
	/// </summary>
	public partial class SalesReceiptPane : BaseUserControl
	{
		public SalesReceiptPane() : this(new SalesReceipt())
		{
			
		}
		
		public SalesReceiptPane(SalesReceipt s)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			Model = s;
		}
		
		protected override void OnModelChanged(EventArgs e)
		{
			base.OnModelChanged(e);
			SalesReceipt s = Model as SalesReceipt;
			s.ItemAdded += delegate { RefreshList(); };
		}
		
		void RefreshList()
		{
			listView1.Items.Clear();
			foreach (SalesReceiptItem i in (Model as SalesReceipt).Items) {
				ListViewItem li = listView1.Items.Add(i.Item.Name);
				li.SubItems.Add(i.Quantity.ToString("0.00"));
			}
		}
		
		void ButtonLogOutClick(object sender, EventArgs e)
		{
			MainForm.Instance.AddChild(new LoginPane());
		}
		
		void ButtonCancelTransactionClick(object sender, EventArgs e)
		{
			Model = new SalesReceipt();
		}
		
		void ButtonAddItemClick(object sender, EventArgs e)
		{
			IList<Item> items = SessionFacade.FindItemsByCode(textBoxCode.Text);
			if (items.Count == 0) {
				MessageService.ShowInfo("The item you searched is not found in the database. Please try again!");
			} else if (items.Count == 1) {
				SalesReceipt s = Model as SalesReceipt;
				s.AddItem(new SalesReceiptItem(items[0], ConvertUtility.ToDouble(textBoxQuantity.Text)));
			} else {
				
			}
			textBoxCode.Clear();
		}
		
		void TextBoxCodeKeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter) {
				buttonAddItem.PerformClick();
			}
		}
	}
}
